package com.tcgroup.phone.base_core.framework.mvvm.activity;

import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;

import com.tcgroup.phone.base_core.framework.mvvm.viewmodel.BaseRefreshViewModel;
import com.tcgroup.phone.base_core.widget.SSmartRefreshLayout;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/18.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm.activity
 * 类名: OSAndroidMvvm
 */
public abstract class BaseMvvmRefreshActivity<V extends ViewDataBinding, VM extends BaseRefreshViewModel> extends ContainerMvvmActivity<V, VM> {

    protected SSmartRefreshLayout mSmartRefreshLayout;

    @Override
    public void initContentView() {
        super.initContentView();
        initRefreshView();
        initBaseViewRefreshObservable();
    }

    public void initRefreshView() {
        mSmartRefreshLayout = getRefreshLayout();
    }

    public void initBaseViewRefreshObservable(){
        mViewModel.getUCRefresh().getRefresLiveEvent().observe(this, (Observer<Boolean>) o -> {
            finishRefresh(o);
        });
        mViewModel.getUCRefresh().getLoadMoreLiveEvent().observe(this,(Observer<Boolean>)o->{
            finishLoadMore(o);
        });
        mViewModel.getUCRefresh().getLoadMoreWithNoMoreLiveEvent().observe(this,o->{
            finishLoadMoreWithNoMoreData();
        });
        mViewModel.getUCRefresh().getAutoRefresLiveEvent().observe(this,o->{
            autoRefresh();
        });
        mViewModel.getUCRefresh().getAutoLoadMoreLiveEvent().observe(this,o->{
            autoLoadMore();
        });
    }

    /**
     * 刷新是否成功
     * @param flag
     */
    public void finishRefresh(boolean flag){
        mSmartRefreshLayout.finishRefresh(flag);
    }

    /**
     * 加载是否成功
     * @param flag
     */
    public void finishLoadMore(boolean flag){
        mSmartRefreshLayout.finishLoadMore(flag);
    }

    /**
     * 加载完成请求已经无数据
     */
    public void finishLoadMoreWithNoMoreData(){
        mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
    }

    /**
     * 自动刷新
     */
    public void autoRefresh(){
        mSmartRefreshLayout.autoRefresh();
    }

    /**
     * 自动加载
     */
    public void autoLoadMore(){
        mSmartRefreshLayout.autoLoadMore();
    }

    public abstract SSmartRefreshLayout getRefreshLayout();


}
